<?php
require_once 'config.php';

// 获取最新上架的5本图书
try {
    $stmt = $pdo->query("SELECT * FROM books ORDER BY book_id DESC LIMIT 5");
    $latest_books = $stmt->fetchAll();
} catch(PDOException $e) {
    $_SESSION['flash_message'] = "获取图书信息失败: " . $e->getMessage();
    $_SESSION['flash_type'] = "danger";
    $latest_books = [];
}

// 页面标题
$page_title = "首页 - 图书借阅系统";

// 引入头部
include 'header.php';
?>

<div class="jumbotron">
    <div class="container">
        <div class="row">
            <div class="col-md-8">
                <h1 class="display-4 font-weight-bold">欢迎使用图书借阅系统</h1>
                <p class="lead">这是一个简单易用的在线图书借阅平台，您可以浏览图书、借阅图书，并管理您的借阅记录。</p>
                <hr class="my-4">
                <p>立即注册成为会员，开始您的阅读之旅！</p>
                <div class="mt-4">
                    <?php if (!isLoggedIn()): ?>
                        <a class="btn btn-light btn-lg mr-2" href="register.php" role="button"><i class="fas fa-user-plus mr-2"></i>注册</a>
                        <a class="btn btn-outline-light btn-lg" href="login.php" role="button"><i class="fas fa-sign-in-alt mr-2"></i>登录</a>
                    <?php else: ?>
                        <a class="btn btn-light btn-lg" href="books.php" role="button"><i class="fas fa-book mr-2"></i>浏览图书</a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="col-md-4 d-none d-md-block">
                <div class="text-center">
                    <i class="fas fa-books fa-book-reader" style="font-size: 180px; opacity: 0.8;"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="container">
    <div class="row align-items-center mb-4">
        <div class="col-md-6">
            <h2 class="mb-0"><i class="fas fa-star mr-2 text-warning"></i>最新上架</h2>
        </div>
        <div class="col-md-6 text-right">
            <a href="books.php" class="btn btn-outline-primary">查看全部图书 <i class="fas fa-arrow-right ml-1"></i></a>
        </div>
    </div>

    <div class="row">
        <?php if (empty($latest_books)): ?>
            <div class="col-12">
                <div class="alert alert-info">
                    <i class="fas fa-info-circle mr-2"></i> 暂无图书信息
                </div>
            </div>
        <?php else: ?>
            <?php foreach ($latest_books as $book): ?>
                <div class="col-md-4 mb-4">
                    <div class="card book-card">
                        <div class="card-body">
                            <h5 class="card-title"><?php echo htmlspecialchars($book['title']); ?></h5>
                            <h6 class="card-subtitle mb-2 text-muted"><i class="fas fa-user-edit mr-1"></i><?php echo htmlspecialchars($book['author']); ?></h6>
                            <p class="card-text">
                                <small class="text-muted"><i class="fas fa-tag mr-1"></i> 分类: <?php echo htmlspecialchars($book['category']); ?></small><br>
                                <small class="text-muted"><i class="fas fa-building mr-1"></i> 出版社: <?php echo htmlspecialchars($book['publisher']); ?></small><br>
                                <small class="text-muted"><i class="fas fa-calendar-alt mr-1"></i> 出版年份: <?php echo htmlspecialchars($book['publication_year']); ?></small>
                            </p>
                            <p class="card-text">
                                <?php if ($book['available'] > 0): ?>
                                    <span class="badge badge-success"><i class="fas fa-check-circle mr-1"></i> 可借阅</span>
                                <?php else: ?>
                                    <span class="badge badge-secondary"><i class="fas fa-times-circle mr-1"></i> 已借完</span>
                                <?php endif; ?>
                            </p>
                            <div class="mt-3">
                                <a href="book_detail.php?id=<?php echo $book['book_id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-info-circle mr-1"></i> 查看详情</a>
                                <?php if (isLoggedIn() && $book['available'] > 0): ?>
                                    <a href="borrow.php?id=<?php echo $book['book_id']; ?>" class="btn btn-sm btn-primary"><i class="fas fa-hand-holding mr-1"></i> 借阅</a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <h2 class="mb-4 mt-5"><i class="fas fa-lightbulb mr-2 text-warning"></i>服务特色</h2>

    <div class="row mt-4">
        <div class="col-md-4 mb-4">
            <div class="card h-100">
                <div class="card-body text-center">
                    <div class="mb-3">
                        <i class="fas fa-book fa-3x text-primary"></i>
                    </div>
                    <h5 class="card-title">丰富的图书资源</h5>
                    <p class="card-text">我们提供各类图书，包括计算机、文学、数学、语言等多个领域的优质读物。</p>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-4">
            <div class="card h-100">
                <div class="card-body text-center">
                    <div class="mb-3">
                        <i class="fas fa-sync-alt fa-3x text-success"></i>
                    </div>
                    <h5 class="card-title">便捷的借阅流程</h5>
                    <p class="card-text">只需简单几步，即可完成图书借阅，系统会自动跟踪您的借阅状态。</p>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-4">
            <div class="card h-100">
                <div class="card-body text-center">
                    <div class="mb-3">
                        <i class="fas fa-user-graduate fa-3x text-info"></i>
                    </div>
                    <h5 class="card-title">个性化的阅读体验</h5>
                    <p class="card-text">根据您的借阅历史，我们会推荐您可能感兴趣的图书，提升您的阅读体验。</p>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?> 